//
//  BackOffBackLogViewController.m
//  代办事宜
//
//  Created by wang on 12-8-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BackOffBackLogViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h"
#import "MBProgressHUD.h"
#import "MainViewController.h"


@interface BackOffBackLogViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,MBProgressHUDDelegate,ASIHTTPRequestDelegate>

@property (nonatomic, strong) MBProgressHUD* mbprogress;
@property (nonatomic, strong) UIAlertView *alertSuccess;

@end

@implementation BackOffBackLogViewController

@synthesize mbprogress=_mbprogress;
@synthesize alertSuccess=_alertSuccess;
NSArray* backOffArray;
NSString* bacfOffEventDtlId;
NSArray* backOffResultArray;


- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.cornerRadius = 10;    //设置弹出框为圆角视图
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 3;   //设置弹出框视图边框宽度
        self.layer.borderColor = [[UIColor brownColor] CGColor];   //设置弹出框边框颜色
        [self setBackgroundColor:[UIColor lightGrayColor]];
        
        //选择回退到某操作人
        UIPickerView*backOffPickView=[[UIPickerView alloc] initWithFrame:CGRectZero];
        backOffPickView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        backOffPickView.dataSource = self;
        backOffPickView.delegate = self; 
        backOffPickView.frame = CGRectMake(0, 30, 740, 200);        
        backOffPickView.showsSelectionIndicator = YES; 
        [backOffPickView setBackgroundColor:[UIColor cyanColor]];
        [self addSubview:backOffPickView];
        
        UIButton* tempSubmitButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        tempSubmitButton.frame=CGRectMake(80, 260, 200, 60);
        [tempSubmitButton setTitle:@"确定" forState:UIControlStateNormal];
        [tempSubmitButton addTarget:self action:@selector(onClickButton4Submmit:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tempSubmitButton];
        
        UIButton* tempCannelButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        tempCannelButton.frame=CGRectMake(450, 260, 200, 60);
        [tempCannelButton setTitle:@"取消" forState:UIControlStateNormal];
        [tempCannelButton addTarget:self action:@selector(onClickButton4Cannel:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tempCannelButton];
        
    }
    return self;
}

//确定
-(void)onClickButton4Submmit:(id)sender{
    
    NSString* u=[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"];
    NSURL *url = [NSURL URLWithString:u];    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];    
    [request addPostValue: @"rollbackEvent" forKey:@"method"];
    [request addPostValue: bacfOffEventDtlId forKey:@"eventDtlId"];
    [request setDelegate:self];
    [self showMbprogress];
    [request start];
}


//取消
-(void)onClickButton4Cannel:(id)sender{
   // self.hidden=YES;    
	[UIView animateWithDuration:1.0 animations:^{
		self.alpha = 0.0f;	
	}];
}

-(void) passBackOffFromSuperView:(NSArray*)arr{
    
    backOffArray=arr;
}

//回退
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{    
    return [backOffArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary*temp=[backOffArray objectAtIndex:row];
    return [[NSString alloc] initWithFormat:@"%@",[temp objectForKey:@"userName"]];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    NSDictionary*temp=[backOffArray objectAtIndex:row];
    NSString* eventDtlId=[temp objectForKey:@"eventDtlId"]; 
    bacfOffEventDtlId=eventDtlId;
    NSLog(@"%@ is selected.",eventDtlId);
}

-(void) showMbprogress {
    
    self.mbprogress = [[MBProgressHUD alloc]initWithFrame:self.bounds];
    [self.mbprogress setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];
    self.mbprogress.labelText = @"正在回退";
    self.mbprogress.delegate = self;
    [self addSubview:self.mbprogress];
    [self.mbprogress show:YES];
    
}

-(void) hideMbProgressSuccess {
    [self.mbprogress hide:YES];
    self.alertSuccess = [[UIAlertView alloc] initWithTitle:@"事务回退" message:@"回退成功!"
                                                   delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",nil];
    [self.alertSuccess show];
}

-(void) hideMbProgressFail {
    [self.mbprogress hide:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"事务回退" message:@"回退失败!请稍后再试"
                                                   delegate:self cancelButtonTitle:nil otherButtonTitles: @"OK",nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView == self.alertSuccess)
    {
        //回代办首页
        [[self viewController].navigationController popToRootViewControllerAnimated:YES];
     }
}


- (void)requestFinished:(ASIHTTPRequest *)request {
    
    NSError *error = [request error];
    if (!error) {
        NSString *res = [request responseString];
        if([res isEqualToString:@"true"])
        {
            [self hideMbProgressSuccess];
        }else{
            [self hideMbProgressFail];
        }
        
    }    
}

//根据view得到controll
- (UIViewController*)viewController {   
    for (UIView* next = [self superview]; next; next = next.superview) {    
        UIResponder* nextResponder = [next nextResponder];     
        if ([nextResponder isKindOfClass:[UIViewController class]]) {      
            return (UIViewController*)nextResponder;
        }   
    }
    return nil; 
}


@end
